<?php
namespace Home\Controller;
use  Think\Controller;

class ParentController extends CommonController{
   // 显示申请记录
    public function index(){

        $id = session(C(SESSIONUSER));
        $res_user = M('user')->where('id = "'.$id.'"')->find();

        $level = $res_user['level'];
        if($level == 1){
            $res_user['level'] = '普通会员';
        }else if($level == 2){
            $res_user['level'] = 'vip会员';
        }else if($level == 3){
            $res_user['level'] = '天使合伙人';
        }else{
            $res_user['level'] = '梦想合伙人';
        }
        $table1 = 'detail'.$level;
        $table2 = 'pic'.$level;

        $res_detail = M($table1)->where("id = '".$res_user['detailid']."'")->find();
        $res_pic = M($table2)->where("id = '".$res_user['picid']."'")->find();

        if($res_user['pid'] != 0){

            $res2 = M('user')->where('id ="'.$res_user['pid'].'"')->find();
            $res_user['parent'] = $res2['user'];
        }else{
            $res_user['parent'] = '无';
        }

        $res_user['phone'] = $res_detail['phone'];
        $res_user['addres'] = $res_detail['addres'];

        $res_num = M('cutnum')->group('userId')->field('sum(num) as number,userId')->order('number')->select();

        foreach($res_num as $k=>$v){

            if($id == $v['userid']){
                $res_user['num'] = $v['number'];
                $res_user['rank'] = $k+1;
            }
        }

        $where['num'] = array('LT','0');
        $where['userId'] = array('eq',$id);

        $res3 = M('cutnum')->where($where)->field('sum(num) as number')->select();
        $res4 = M('cutnum')->where('userId = "'.$id.'"')->field('sum(num) as number')->select();

        $res_user['cnum'] = abs($res3[0]['number'])+ abs($res4[0]['number']);
        $res_user['xnum'] = abs($res3[0]['number']);// 已用名额
        $res_user['snum'] = abs($res4[0]['number']);// 剩余名额

        $res_notice1 = M('notice2')->where('userId = "'.$id.'" and allow = 1')->select();
        $res_notice2 = M('notice3')->where('userId = "'.$id.'" and allow = 1')->select();



        $notice = [];
        foreach($res_notice1 as $k=>$v){

            $notice[] = $v;
        }

        foreach($res_notice2 as $k=>$v){

            $notice[] = $v;
        }

        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        foreach($notice as $k=>$v){
            $notice[$k]['level'] = $getlevel[$v['level']];
            $notice[$k]['time'] = date('Y-m-d',$v['time']);
            $ids1[] = $v['objectid'];
        }

        $ids1 = array_unique($ids1);
        $ids1 = implode(',',$ids1);

        if(!$ids1){
            $this->assign('list',$res_user);
            $this->display();
            die;
        }

        $res_user3 = M('user')->where('id in ('.$ids1.')')->select();

        foreach($notice as $k=>$v){

            foreach($res_user3 as $k1=>$v1){

                if($v['objectid'] == $v1['id']){

                    $notice[$k]['user'] = $v1['user'];
                }
            }
        }

        // 导入分页类
        import('Org.Util.Page');

        //查询满足满足条件的总记录条数
        $count = count($notice);

        //实例化分页类 传入总记录数和每页显示的记录数
        $Page = new \Think\Page($count,2);

        //分页显示输出
        $show = $Page->show();

//        $limit = $Page->firstRow . ',' . $Page->listRows;
        $star = $Page->firstRow;
        $end = $Page->listRows+$Page->firstRow-1;

        foreach($notice as $k=>$v){

            if($k>=$star && $k<=$end){
                $new_notice[] = $v;
            }
        }


        $this->assign('notice',$new_notice);
        $this->assign('list',$res_user);
        $this->assign('page',$show);
        $this->display();
    }

    // 显示代理商
    public function wddls(){

        $id = session(C(SESSIONUSER));
        $res_user = M('user')->where('id = "'.$id.'"')->find();

        $level = $res_user['level'];
        if($level == 1){
            $res_user['level'] = '普通会员';
        }else if($level == 2){
            $res_user['level'] = 'vip会员';
        }else if($level == 3){
            $res_user['level'] = '天使合伙人';
        }else{
            $res_user['level'] = '梦想合伙人';
        }
        $table1 = 'detail'.$level;
        $table2 = 'pic'.$level;

        $res_detail = M($table1)->where("id = '".$res_user['detailid']."'")->find();
        $res_pic = M($table2)->where("id = '".$res_user['picid']."'")->find();

        if($res_user['pid'] != 0){

            $res2 = M('user')->where('id ="'.$res_user['pid'].'"')->find();
            $res_user['parent'] = $res2['user'];
        }else{
            $res_user['parent'] = '无';
        }

        $res_user['phone'] = $res_detail['phone'];
        $res_user['addres'] = $res_detail['addres'];

        $res_num = M('cutnum')->group('userId')->field('sum(num) as number,userId')->order('number')->select();

        foreach($res_num as $k=>$v){

            if($id == $v['userid']){
                $res_user['num'] = $v['number'];
                $res_user['rank'] = $k+1;
            }
        }

        $where['num'] = array('LT','0');
        $where['userId'] = array('eq',$id);

        $res3 = M('cutnum')->where($where)->field('sum(num) as number')->select();
        $res4 = M('cutnum')->where('userId = "'.$id.'"')->field('sum(num) as number')->select();

        $res_user['cnum'] = abs($res3[0]['number'])+ abs($res4[0]['number']);
        $res_user['xnum'] = abs($res3[0]['number']);// 已用名额
        $res_user['snum'] = abs($res4[0]['number']);// 剩余名额

        // 导入分页类
        import('Org.Util.Page');

        $list = M('user')->where('pid = "'.$id.'"')->select();

        //查询满足满足条件的总记录条数
        $count = count($list);

        //实例化分页类 传入总记录数和每页显示的记录数
        $Page = new \Think\Page($count,2);

        //分页显示输出
        $show = $Page->show();

        $limit = $Page->firstRow . ',' . $Page->listRows;

        $res_user3 = M('user')->where('pid = "'.$id.'"')->limit($limit)->select();

        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        foreach($res_user3 as $k=>$v){
            $res_user3[$k]['level'] = $getlevel[$v['level']];
        }

        $this->assign('user',$res_user3);
        $this->assign('list',$res_user);
        $this->assign('page',$show);
        $this->display();

    }

    public function allow(){

        $uid = session(C(SESSIONUSER));
        $id = I('id');
        $level1 = I('level');
       $getlevel = ['普通会员'=>1,'vip会员'=>2,'天使合伙人'=>3,'梦想合伙人'=>4];

       $level = $getlevel[$level1];

//        $applyId = I('applyid');
//        $objectId = I('objectid');
        $table5 = 'notice'.$level;
        $res_notice2 =M($table5)->where('id = "'.$id.'"')->find();
        $applyId = $res_notice2['applyid'];
        $objectId = $res_notice2['objectid'];

//        var_dump($res_notice2);die;

        $res_num = M('cutnum')->where('userId = "'.$uid.'"')->field('sum(num) as number')->select();
        $res_user = M('user')->where('id = "'.$uid.'"')->find();
        $table3 = 'userstatus'.$res_user['level'];

        $res_status3 = M($table3)->where('userId = "'.$uid.'"')->find();

        if($res_status3){
            if($res_status3['status'] ==2){
//                $data = genBody('0','您的名额不足，请先升级');
//                $this->ajaxReturn($data,'JSON');
                $this->redirect('Money/money','','1','您的名额不足，请升级或重新购买');
            }
        }

        if($level == 2){
            if($res_num[0]['number'] <1){
                // 发送系统公告
                // 出现错误情况时  发送要求升级的公告
                // 当合伙人点击允许的时候，判断名额不足时，弹出公告 询问是否升级 或者购买名额
                $res_notice = $this->sendnotice(2,$uid);
                $res_user = M('user')->where("id = '".$uid."'")->find();

                $status_user = $this->setstatus($uid,2,$res_user['level']);

                if($res_user['level'] == 4){
                    // 当上级为梦想合伙人时候，会使该升级用户进入判断状态，如果到时间梦想合伙人未升级，则由平台指定。
                    $arr_list['userId'] = $objectId;
                    $arr_list['pid'] = $uid;
                    $arr_list['level'] = $level;
                    $arr_list['starTime'] = time();
                    $arr_list['endTime'] = time()+24*60*60;
                    $arr_list['status'] = 1;
                    $arr_list['state'] = 1;

                    $res_status  = M('status')->add($arr_list);
                }else{

                    $arr_list['userId'] = $objectId;
                    $arr_list['pid'] = $uid;
                    $arr_list['starTime'] = time();
                    $arr_list['endTime'] = time()+24*60*60;

                    $res_status  = M('status3')->add($arr_list);
                }
                // 后台申请表 状态修改
                $arr_apply1['allow'] = 4;// 暂时停止操作
                $arr_apply1['reson'] = 2; // 该上级名额不足
                $table = 'apply'.$level;

                $res_apply_save = M($table)->where('id ="'.$applyId.'"')->save($arr_apply1);

                if($res_apply_save){

                    $this->redirect('Money/money','','1','您的名额不足，请升级或重新购买');
                }

            }else{
                // 点击允许
                $table = 'notice'.$level;
                $arr_allow['allow'] = 2;// 确定
                $res_notice_allow = M($table)->where('id ="'.$id.'"')->save($arr_allow);

                $arr_apply['view'] = 1;// 上级允许升级
                $arr_apply['allow'] = 2;// 允许升级
                $table = 'apply'.$level;
                $res_apply_save2 = M($table)->where('id ="'.$applyId.'"')->save($arr_apply);

                // 点击允许时  如果申请者有过失败记录 则修改失败记录
                $res_check_status = $this->checkstatus1($res_user,$objectId,$uid);
                //升级为vip 合伙人可以自己判断决定
                $arr_num['num'] = -1;
                $arr_num['userId'] = $uid;
                $arr_num['geterId'] = $objectId;

                $res_num = M('cutnum')->add($arr_num);

                if($res_num){

                    // 用户升级
                    $res_user_level = $this->setlevel($objectId,$level);
                    if($res_user_level){


                        $this->redirect('Parent/index','','1','操作成功');
                    }
                }

            }
        }else if($level == 3){

            // 如果是要升级为天使合伙人
            // 判断是什么登记
            // 如果是梦想  允许后判断是否有名额
            // 没有 发送公告要求升级 更改申请表的状态 更改status表的状态
            $current_id = $_SESSION['user']['userId'];

            $res_user3 = M('user')->where('id = "'.$current_id.'"')->find();
            $current_level = $res_user3['level'];

            if($current_level == 3){

                $status = 3;
                $res_set_status = $this->setstatus($uid,$status,$level);// 修改上级状态

                $res_notice1 = $this->sendnotice(4,$uid); // 发送邀请升级的公告

                // 更改申请表状态
                $arr_apply1['allow'] = 4;// 暂时停止操作
                $arr_apply1['reson'] = 3; // 该上级权限不足
                $table = 'apply'.$level;

                $res_apply_save = M($table)->where('id ="'.$applyId.'"')->save($arr_apply1);

                $arr_list1['userId'] = $objectId;
                $arr_list1['pid'] = $uid;
                $arr_list1['starTime'] = time();
                $arr_list1['endTime'] = time()+24*60*60;

                $res_status  = M('status3')->add($arr_list1);

                $this->redirect('Money/money','','3','您有一个vip需要升级为天使，请您先去升级为梦想合伙人');

//                $data = genBody('1','您有一个vip需要升级为天使，请您先去升级');
//                $this->ajaxReturn($data,'JSON');

            }else if($current_level == 4){

                $check_level = M('user')->where("id = '".$objectId."'")->find();
                if($check_level['level'] == 2){
                    $num = 19;
                }else{
                    $num = 20;
                }

                if($res_num[0]['number'] < $num){

                    $res_notice1 = $this->sendnotice(3,$uid);
                    // 修改状态
                    $status = 3;
                    $res_set_status = $this->setstatus($uid,$status,$level);// 修改上级状态

                    $arr_list['userId'] = $objectId;
                    $arr_list['pid'] = $uid;
                    $arr_list['level'] = $level;
                    $arr_list['starTime'] = time();
                    $arr_list['endTime'] = time()+24*60*60;
                    $arr_list['status'] = 1;
                    $arr_list['state'] = 1;

                    $res_status  = M('status')->add($arr_list);

                    // 后台申请表 状态修改
                    $arr_apply1['allow'] = 4;// 暂时停止操作
                    $arr_apply1['reson'] = 2; // 该上级名额不足
                    $table = 'apply'.$level;

                    $res_apply_save = M($table)->where('id ="'.$applyId.'"')->save($arr_apply1);
                    $this->redirect('Money/money','','3','您的剩余名额不足以接收天使合伙人，请购买名额');

                }else{

                    // 点击允许
                    $table = 'notice'.$level;
                    $arr_allow['allow'] = 2;// 确定
                    $res_notice_allow = M($table)->where('id ="'.$id.'"')->save($arr_allow);

                    $arr_apply['view'] = 1;// 上级允许升级
                    $arr_apply['allow'] = 2;// 允许升级
                    $table = 'apply'.$level;
                    $res_apply_save2 = M($table)->where('id ="'.$applyId.'"')->save($arr_apply);

                    // 点击允许时  如果申请者有过失败记录 则修改失败记录
                    $res_check_status = $this->checkstatus1($res_user,$objectId,$uid);

                    if($res_apply_save2){

                        $this->redirect('Parent/index','','3','您的决定已经提交，请耐心等待客服的回访');

//                        $data = genBody('1','您的决定已经提交，请耐性等待客服的回访');
//                        $this->ajaxReturn($data,'JSON');
                    }
                }


            }
        }


    }

    public function forbidden(){
        $uid = session(C(SESSIONUSER));
        $id = I('id');
        $level = I('level');
//        $applyId = I('applyid');
//        $objectId = I('objectid');
        $table5 = 'notice'.$level;
        $res_notice2 =M($table5)->where('id = "'.$id.'"')->find();
        $applyId = $res_notice2['applyid'];
        $objectId = $res_notice2['objectid'];

        // 上级不允许
        // 1 修改notice
        // 2 修改apply
        $arr_notice['allow'] = 3;// 不允许
        $save_notice = M($table5)->where9('id = "'.$id.'"')->save($arr_notice);

        $table_apply = 'apply'.$level;
        $arr_apply['view'] = 2; // 不允许
        $arr_apply['allow'] = 4;// 暂时停止

        $save_apply = M($table_apply)->where('id = "'.$applyId.'"')->save($arr_apply);

        if($save_apply && $save_notice){

            $this->redirect('Parent/index','','1','操作成功');
        }else{
            $this->redirect('Parent/index','','1','操作失败');

        }
    }


    public function checkstatus1($res_user,$objectId,$uid){

        if($res_user['level'] == 4){
            $where_status['userId'] = $objectId;
            $where_status['pid'] = $uid;
            $where_status['status'] = 1;

            $res6 = M('status')->where($where_status)->find();
            if($res6){
                $save6['status'] = 2;// 脱离状态
                $res_save6 = M('status')->where('id = "'.$res6['id'].'"')->save($save6);
            }

        }else{
            // 如果该下级有状态记录
            $where_status['userId'] = $objectId;
            $where_status['pid'] = $uid;
            $where_status['task'] = 1;

            $res5 = M('status3')->where($where_status)->find();

            if($res5){
                $arr_status3['task'] = 2;// 废弃
                $res_save6 = M('status3')->where('id = "'.$res5['id'].'"')->save($arr_status3);
            }

        }

        return 1;
    }

    public function sendnotice($noticeId,$userId){

        $arr_notice['noticeId'] = $noticeId;// 名额不足
        $arr_notice['userId'] = $userId;
        $arr_notice['time'] = time();
        // 当该上级名额或者权限不足时，发送的公告
        $res_notice = M('notice')->add($arr_notice);

        if($res_notice){
            return 1;
        }
    }

    // 当上级名额不足或者权限不够时，设置userstatus表的状态
    // $userId 上级的id;
    // $status  想要修改的状态
    // $level  上级的等级
    public function setstatus($userId,$status,$level){

        $table = 'userstatus'.$level;
        $res_select = M($table)->where("userId = '".$userId."'")->find();

        $arr_userstatus['userId'] = $userId;
        $arr_userstatus['status'] = $status;

        // 修改状态
        if($res_select) {

            $res_save_status = M($table)->where("userId = '".$userId."'")->save($arr_userstatus);

        }else{

            $res_save_status = M($table)->where("userId = '" . $userId . "'")->add($arr_userstatus);
        }

        return $res_save_status;
    }


    public function setlevel($id,$level){

        $save_level['level'] = $level;

        $pid = session(C(SESSIONUSER));
        $save_level['pid'] = $pid;

        $result = M('user')->where('id = "'.$id.'"')->find();

        $table_detail = 'detail'.$result['level'];
        $res_detail = M($table_detail)->where("id = '".$result['detailid']."'")->find();

        unset($res_detail['id']);

        $table_pic = 'pic'.$result['level'];
        $res_pic = M($table_pic)->where("id = '".$result['picid']."'")->find();

        unset($res_pic['id']);

        $table1 = 'detail'.$level;
        $table2 = 'pic'.$level;

        $res_detail['userId'] = $res_detail['userid'];
        $res_detail['useredId'] = $res_detail['useredid'];
        $res_pic['userId'] = $res_pic['userid'];

        $de_insert = M($table1)->add($res_detail);
        $pic_insert = M($table2)->add($res_pic);

        $save_level['picId'] = $pic_insert;
        $save_level['detailId'] = $de_insert;

        if($res_detail['useredId'] != $save_level['pid']){

            $arr_notice['noticeId'] = 7;// 返佣
            $arr_notice['userId'] = $res_detail['useredId'];
            $arr_notice['time'] = time();

            $res_notice3 = M('notice')->add($arr_notice);
        }

//        var_dump($save_level);die;

        $user_insert = M('user')->where('id = "'.$id.'"')->save($save_level);

        if($user_insert){

            return 1;
        }
    }
}